package com.hrma.controller;

import com.github.pagehelper.Page;
import com.hrma.entity.*;
import com.hrma.services.PenaltyService;
import com.hrma.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/penalty")
@Slf4j
public class PenaltyController {
    @Autowired
    private final PenaltyService penaltyService;

    public PenaltyController(PenaltyService penaltyService) {
        this.penaltyService = penaltyService;
    }

    @PostMapping("/add")
    public Result<Penalty> add(@Validated @RequestBody Penalty data) {
        log.debug("收到add请求，请求参数：{}", data);
        return Result.ok(penaltyService.add(data));
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable int id) {
        log.debug("收到delete请求，请求参数：{}", id);
        penaltyService.delete(id);
        return Result.ok(null);
    }

    @PatchMapping("/update")
    public Result<?> update(@RequestBody Penalty data) {
        log.debug("收到update请求，请求参数：{}", data);
        penaltyService.update(data);
        return Result.ok(null);
    }

    @GetMapping("/select")
    public Result<List<SelectResult>> select() {
        log.debug("收到select请求！");
        return Result.ok(penaltyService.select());

    }

    @PostMapping("/list")
    public Result<ListWithCount<Penalty>> getList(@RequestBody PenaltyDTO data) {
        log.debug("收到list请求，请求参数：{}", data);
        Page<Penalty> res = penaltyService.getList(data);

        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }
}
